package com.example.demo.service;

import com.example.demo.dao.mapper.AcMapper;
import com.example.demo.dao.mapper.ProblemMapper;
import com.example.demo.dao.pojo.AcInfo;
import com.example.demo.dao.pojo.ProblemInfo;
import com.example.demo.dao.pojo.UserInfo;
import com.example.demo.vo.*;
import com.example.demo.vo.params.SearchParams;
import com.example.demo.vo.params.UpdateProblemParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProblemService {

    @Autowired
    private ProblemMapper problemMapper;

    @Autowired
    private AcMapper acMapper;

    @Autowired
    private TokenService tokenService;


    public int getCountExit(String question){
        return problemMapper.getCountExit(question);
    }
    public int getCountAll(String question){
        return problemMapper.getCountAll(question);
    }

    // 1. 获取题目列表（id、标题、访问次数、ac 次数、难度）
//    public Result listProblem(PageParams params){
//        Integer page = params.getPage();
//        Integer pageSize = params.getPageSize();
//        String token = params.getToken();
//        UserInfo userInfo = tokenService.checkToken(token);
//        List<ProblemInfo> records = problemMapper.selectAll((page-1)*pageSize, pageSize);
//        if(records == null){
//            return Result.fail(ErrorCode.PROBLEM_NOT_EXISTS.getCode(), ErrorCode.PROBLEM_NOT_EXISTS.getMsg());
//        }
//        List<ProblemVo> problemVos = new ArrayList<>();
//        if(userInfo == null) {
//            problemVos = copyList(records, -1);
//        }else{
//            problemVos = copyList(records, userInfo.getId());
//        }
//        return Result.success(problemVos);
//    }


    // 3. 新增题目
    public int addProblem(ProblemInfo problemInfo){
        return problemMapper.add(problemInfo);
    }

    // 4. 删除题目
    public int removeProblem(int id){
        return problemMapper.remove(id);
    }


    private List<ProblemVo> copyList(List<ProblemInfo> records, Integer userId, int count){
        List<ProblemVo> problemVoList = new ArrayList<>();
        for (ProblemInfo record : records) {
            problemVoList.add(copy(record, userId, count));
        }
        return problemVoList;
    }

    private ProblemVo copy(ProblemInfo problemInfo, Integer useId, int count){
        ProblemVo problemVo = new ProblemVo();
        BeanUtils.copyProperties(problemInfo, problemVo);
        AcInfo acInfo = acMapper.selectOne(problemVo.getId(), useId);
        if(acInfo != null){
            problemVo.setAcState(1);
        }
        problemVo.setCount(count);
        return problemVo;
    }

    public Result findProblemById(int id) {
        ProblemInfo problemInfo = problemMapper.selectOneById(id);
        if(problemInfo == null){
            return Result.fail(ErrorCode.PROBLEM_NOT_EXISTS.getCode(), ErrorCode.PROBLEM_NOT_EXISTS.getMsg());
        }
        return Result.success(problemInfo);
    }

    public Result selectByLike(SearchParams params) {
        Integer page = params.getPage();
        Integer pageSize = params.getPageSize();
        String question = params.getQuestion();
        String token = params.getToken();
        int count = getCountExit(question);
        if(question == null){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }
        UserInfo userInfo = tokenService.checkToken(token);
        if(question.equals("已完成") && userInfo != null){
            count = acMapper.getAcCount(userInfo.getId());
            List<Integer> acPIDs = acMapper.selectByUserID(userInfo.getId(), (page-1)*pageSize, pageSize);
            List<ProblemInfo> problemInfoList = new ArrayList<>();
            for (Integer pid : acPIDs) {
                ProblemInfo problemInfo = problemMapper.selectOneById(pid);
                problemInfoList.add(problemInfo);
            }
            List<ProblemVo> problemVos = copyList(problemInfoList, userInfo.getId(), count);
            return Result.success(problemVos);
        }else if(question.equals("题目管理") && userInfo != null){
            count = getCountAll("");
            List<ProblemInfo> problemInfoList = problemMapper.selectByLike("", (page-1)*pageSize, pageSize);
            if (problemInfoList == null) {
                return Result.fail(ErrorCode.PROBLEM_NOT_EXISTS.getCode(), ErrorCode.PROBLEM_NOT_EXISTS.getMsg());
            }
            AdminProblemVo adminProblemVo = new AdminProblemVo();
            adminProblemVo.setProblemInfoList(problemInfoList);
            adminProblemVo.setCount(count);
            return Result.success(adminProblemVo);
        } else{
            List<ProblemInfo> problemInfoList = problemMapper.selectByLike(question, (page-1)*pageSize, pageSize);
            if (problemInfoList == null) {
                return Result.fail(ErrorCode.PROBLEM_NOT_EXISTS.getCode(), ErrorCode.PROBLEM_NOT_EXISTS.getMsg());
            }
            List<ProblemVo> problemVos = new ArrayList<>();
            if (userInfo == null) {
                problemVos = copyList(problemInfoList, -1, count);
            } else {
                problemVos = copyList(problemInfoList, userInfo.getId(), count);
            }
            return Result.success(problemVos);
        }
    }

    public Result deleteProblem(int id) {
        int res = problemMapper.deleteById(id);
        if(res == 1){
            return Result.success(null);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
    }

    public Result updateProblem(UpdateProblemParams params) {
        int id = params.getId();
        int result = 0;
        if(id == 0){
            ProblemInfo problemInfo = new ProblemInfo();
            problemInfo.setTitle(params.getTitle());
            problemInfo.setLevel(params.getLevel());
            problemInfo.setDescription(params.getDesc());
            problemInfo.setTemplateCode(params.getModelCoding());
            problemInfo.setTestCode(params.getTestCoding());
            result = problemMapper.add(problemInfo);
        }else if(problemMapper.selectOneById(id) != null){
            result = problemMapper.update(params);
        }
        if(result == 1){
            return Result.success(null);
        }
        return Result.fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
    }
}
